package com.qinggeng.project.system.controller;

import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.SysRegion;
import com.qinggeng.project.system.service.ISysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地区Controller
 *
 * @author qinggeng
 * @date 2020-06-18
 */
@RestController
@RequestMapping("/system/sysregion")
public class SysRegionController extends BaseController {
    @Autowired
    private ISysRegionService sysRegionService;

    /**
     * 查询地区列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysregion:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRegion sysRegion) {
        startPage();
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegion);
        return getDataTable(list);
    }

    /**
     * 导出地区列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysregion:export')")
    @Log(title = "地区", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysRegion sysRegion) {
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegion);
        ExcelUtil<SysRegion> util = new ExcelUtil<SysRegion>(SysRegion.class);
        return util.exportExcel(list, "region");
    }

    /**
     * 获取地区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysregion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysRegionService.selectSysRegionById(id));
    }

    /**
     * 新增地区
     */
    @PreAuthorize("@ss.hasPermi('system:sysregion:add')")
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRegion sysRegion) {
        return toAjax(sysRegionService.insertSysRegion(sysRegion));
    }

    /**
     * 修改地区
     */
    @PreAuthorize("@ss.hasPermi('system:sysregion:edit')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegion sysRegion) {
        return toAjax(sysRegionService.updateSysRegion(sysRegion));
    }

    /**
     * 删除地区
     */
    @PreAuthorize("@ss.hasPermi('system:sysregion:remove')")
    @Log(title = "地区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysRegionService.deleteSysRegionByIds(ids));
    }


    @GetMapping("/getRegionList")
    @ResponseBody
    public AjaxResult getRegionList(SysRegion sysRegion) {
        List<SysRegion> sysRegions = sysRegionService.selectSysRegionList(sysRegion);
        return AjaxResult.success(sysRegions);
    }

}
